import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { useAuth } from '../context/AuthProvider'

const AuthRoute = ({authCheckType,power,component:Component,routes,...rest}) => {
    const {user} = useAuth();
    // console.log(Object.keys(user),user,authCheckType,rest.path)
    if(authCheckType=='unauthed'&&user.username=='xtadmin'){
        return <Redirect to='/register' />
    }
    if(authCheckType=='authed'&&!Object.keys(user).length){
        return <Redirect to='/login' />
    }
    
    if(authCheckType=='unauthed'&&!!Object.keys(user).length){
        return <Redirect to='/home/home' />
    }
    console.log(power,user)
    if((authCheckType=='authed')&&(user.power!=power)&&(power!=0)){
        if(rest.path!='/power'){
            return <Redirect to='/power' />
        }
       
    }
    
        return (
            <Route 
                render={
                    (props)=><Component {...props} routes={routes} />
                }
                {...rest} 
            />
        )

    
}

export default AuthRoute